<?php

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191); //配置迁移命令migrate生成的默认字符串长度

        //视图全局变量 菜单 & 消息
        view()->composer('admin.layout', function ($view) {
            //左侧菜单
            $menus = \App\Models\Permission::with([
                'childs' => function ($query) {
                    $query->with('icon');
                    $query->with([
                        'childs' => function ($query) {
                            $query->with('icon')
                                ->where('is_menu', 1)
                                ->orderBy('sort', 'desc');
                        }, 'icon'])
                        ->where('is_menu', 1)
                        ->orderBy('sort', 'desc');
                }, 'icon'])
                ->where('parent_id', 0)
                ->where('is_menu', 1)
                ->orderBy('sort', 'desc')
                ->get();

            //当前用户未读的消息
            $unreadMessage = \App\Models\Message::where('read', 1)
                ->where('accept_uuid', auth()->user()->uuid)
                ->count();

            $view->with('menus', $menus);
            $view->with('unreadMessage', $unreadMessage);
        });
    }

    /**
     * Register any application services.
     * @return void
     */
    public function register()
    {
        $this->app->bind('App\Contracts\SmsContract', 'App\Services\SmsService');

        if ($this->app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
    }
}
